﻿const query = require('../client');
const moment = require('moment');
const createGuid = require('../lib/util').createGuid;
const dateToString = require('../lib/util').dateToString;

module.exports = {

    // 删除或恢复班级
    delClass: (guid, State, callback) => {
        let sql = `UPDATE class SET State='${State}' WHERE PK_ClassGuid='${guid}';`;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                callback(0, err);
                return;
            }
            callback(1, vals);
        });
    },
    // 添加班级
    addClass: (data, callback) => {
        let classGuid = createGuid();
        let className = data.className;
        let number = 0;
        let createTime = moment().format('YYYY-MM-DD HH:mm:ss').toString();
        let endTime = data.endTime;
        let state = 0;
        let creatorGuid = data.creatorGuid;
        let coverUrl = data.coverUrl;
        let idCode = data.idCode;

        let sql = `INSERT INTO class(PK_ClassGuid,ClassName,Number,CreateTime,EndTime,State,CreatorGuid,CoverUrl, IDCode) VALUES(
              '${classGuid}','${className}',${number},'${createTime}','${endTime}',${state},'${creatorGuid}','${coverUrl}', '${idCode}');`;
        query(sql, function (err, vals) {
            if (err) {
                console.log(err);
                callback(0, err);
                return;
            }
            callback(1, classGuid);
        });
    },
    // 编辑班级
    putClass: (data, callback) => {
        let sql = `UPDATE class  set  ClassName='${data.className}',EndTime='${data.endTime}',CoverUrl='${data.coverUrl}' 
        where PK_ClassGuid='${data.PK_ClassGuid}'  `
        query(sql, function (err, vals) {
            if (err) {
                console.log(err);
                callback(0, err);
                return;
            }
            callback(1, 'ok');
        });
    },
    // 获取指定班级列表
    getClassesByGuid: (classListString, callback) => {
        let sql = `SELECT *,(select count(*) from lessonplan_user.user_in_class  where lessonplan_user.user_in_class.FK_ClassGuid =lessonplan_class.class.PK_ClassGuid ) as Number
        ,(select veloaccount.user.UserName from veloaccount.user  where  veloaccount.user.PK_UserGuid=lessonplan_class.class.CreatorGuid ) as Creator
              FROM class WHERE PK_ClassGuid IN(${classListString}) and State=0  ORDER BY CreateTime DESC;`;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                callback(0, err);
                return;
            }
            callback(1, vals);
        });
    },
    // 获取全部班级列表
    getAllClasses: (data, callback) => {
        let sql = `SELECT PK_ClassGuid,ClassName,InvitationCode,CreateTime,EndTime,CreatorGuid,IDCode,CoverUrl,Number 
              FROM class WHERE State=0  ORDER BY CreateTime DESC `;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                callback(0, err);
                return;
            }
            callback(1, vals);
        });
    },
    // 获取当前所有可用班级列表 GET class/all
    getClassList: (guid, State, callback) => {
        let sql = `SELECT lessonplan_class.class.*,(select count(*) from lessonplan_user.user_in_class  where lessonplan_user.user_in_class.FK_ClassGuid =lessonplan_class.class.PK_ClassGuid ) as Number FROM lessonplan_class.class   WHERE CreatorGuid='${guid}' and State='${State}' ORDER BY CreateTime DESC ;`;
        query(sql, function(err, vals) {
            if (err) {
                console.log(err);
                callback(0, err);
                return;
            }
            callback(1, vals);
        });
    },
    // 搜索班级 class/search?searchText=&n=&m=
    search: (data, callback) => {
        let sql1 = `SELECT lessonplan_class.class.*,(select count(*) from lessonplan_user.user_in_class  where lessonplan_user.user_in_class.FK_ClassGuid =lessonplan_class.class.PK_ClassGuid ) as Number,veloaccount.user.UserName,veloaccount.user.PK_UserGuid FROM lessonplan_class.class left join veloaccount.user on lessonplan_class.class.CreatorGuid=veloaccount.user.PK_UserGuid 
         WHERE lessonplan_class.class.State=0 and  CONCAT(lessonplan_class.class.ClassName,lessonplan_class.class.IDCode,veloaccount.user.UserName,lessonplan_class.class.PK_ClassGuid)  like '${data.searchText}' limit ${data.n},${data.m} `;
        query(sql1, (err, result) => {
            if (err) {
                console.log(err);
                callback(0, err)
                return;
            }
            callback(1, result)
        });
    },
     join: (data, callback) => {
        let sql1 = `select InvitationCode from class where  PK_ClassGuid ='${data.FK_ClassGuid}'`;
        query(sql1, (err, result) => { 
            if (result[0].InvitationCode != data.InvitationCode) {
                callback(0, '邀请码不对')
            } else {
                let sql3 = `select * from lessonplan_user.user_in_class where FK_UserGuid='${data.FK_UserGuid}' and FK_ClassGuid='${data.FK_ClassGuid}'`
                query(sql3, (err3, result3) => {
                    if (err) {
                        console.log(err);
                        callback(0, err)
                        return;
                    } else {
                        if (result3.length > 0) {
                            callback(0, '已加入了该班级！')
                        } else {
                            let createTime = moment().format('YYYY-MM-DD HH:mm:ss').toString();
                            let sql2 = `
                            INSERT INTO lessonplan_user.user_in_class (PK_UICGuid,FK_UserGuid,FK_ClassGuid,CreateTime ) VALUES (
                                '${createGuid()}','${data.FK_UserGuid}','${data.FK_ClassGuid}','${createTime}')
                            `
                            query(sql2, (err, result) => {
                                if (err) {
                                    console.log(err);
                                    callback(0, err)
                                    return;
                                } else {
                                    callback(1, 'ok')
                                }
                            })
                        }
                    }
                })
            }
        })
    },
		    join2: (data, callback) => {
        let sql3 = `select * from lessonplan_user.user_in_class where FK_UserGuid='${data.FK_UserGuid}' and FK_ClassGuid='${data.FK_ClassGuid}'`
        query(sql3, (err3, result3) => {
            if (err3) {
                console.log(err3);
                callback(0, err3)
                return;
            } else {
                if (result3.length > 0) {
                    callback(0, '已加入了该班级！')
                } else {
                    let createTime = moment().format('YYYY-MM-DD HH:mm:ss').toString();
                    let sql2 = `
                INSERT INTO lessonplan_user.user_in_class (PK_UICGuid,FK_UserGuid,FK_ClassGuid,CreateTime ) VALUES (
                    '${createGuid()}','${data.FK_UserGuid}','${data.FK_ClassGuid}','${createTime}')
                `
                    query(sql2, (err, result) => {
                        if (err) {
                            console.log(err);
                            callback(0, err)
                            return;
                        } else {
                            callback(1, 'ok')
                        }
                    })
                }
            }
        })
    }
};